<?php
/*************************************************

平台控制器 - the
Author: Reload <17-10-25>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class PlatformController extends CommonController {

    //平台详情页
    public function platform_detail() {
        $plat_id = I('get.plat_id');
        $map['id'] = $plat_id;
        //$map['id'] = 8;
        $plat = D('Platform')->get_one($map);
        $map = array();
        $map['platform_id'] = $plat['id'];
        // $plat_scheme = D('PlatformScheme')->get_all($map);
    //    print_r($plat_scheme);
        // foreach($plat_scheme as $k => $v) {
        //     $plat_scheme[$k]['yields'] = $plat['yields'];
        // }
        $plat_obj = D('PlatformObject')->get_all($map);
    //    print_r($plat_obj);
        $plat_map = M('PlatformMap')->where($map)->find();
        $plat['plat_map'] = $plat_map['pic'];
        $plat_team = M('PlatformTeam')->where($map)->select();
        $plat['plat_team'] = $plat_team;
        $plat_assets = M('PlatformAssets')->where($map)->select();
        $plat['plat_assets'] = $plat_assets;
        // $map['house_id'] = array('neq',11);
        $video_app = M('VideoAppraisal')->where($map)->select();
        
        if($video_app){
            $is_content = 1;
            foreach($video_app as $k => $v) {
                if($v['house_id']==11){
                    unset($video_app[$k]);
                }
                $house_id[] = $v['house_id'];
                $map['id'] = array('in',$house_id);
                $video = D('Video')->get_all($map);
            }
        }else{
            $is_content = 0;
        }

        foreach($video as $k => $v) {
            
            foreach($video_app as $ke => $va) {
                if($v['id']==$va['house_id']) {
                    $video[$k]['cate_id'] = $va['cate_id'];
                }
            }
             if($v['id']==11){
                    unset($video[$k]);
                }
        }
        //print_r($video);
        $plat['video'] = $video;
    //    print_r($plat);
    //    
        $this->assign('is_content',$is_content);
        $this->assign('video_app',$video_app);
        $this->assign('plat_obj',$plat_obj);
        $this->assign('plat',$plat);
        $this->display();
    }


    public function jump_to_platform() {
        $id = I('get.plat_id');
        $map['id'] = $id;
        $platform = D('Platform')->get_one($map);
        $map = array();
        if($_SESSION['user']['id']){
            $map['id'] = $_SESSION['user']['id'];
        }else{
            $map['id'] = I('get.userid');
        }
        
        
        $user_info = D('User')->get_one($map);
        if($_SESSION['user']['id']){
            $data['user_id'] =  $_SESSION['user']['id'];
        }else{
            $data['user_id'] =  I('get.userid');
        }
        
        $data['add_time'] = time();
        $data['platform_id'] =$id;
        $data['referral'] =$user_info['referral'];

        $map = array();
        $map['platform_id'] =$id;
        if($_SESSION['user']['id']){
            $data['user_id'] =  $_SESSION['user']['id'];
        }else{
            $data['user_id'] =  I('get.userid');
        }
        $res =  M('UserPlatform')->where($map)->find();
       if(!$res && $_SESSION['user']['id']){
            D('UserPlatform')->add($data);
       }
        $this->assign('platform',$platform);
        $this->assign('user_info',$user_info);
        $this->display();
    }
	/**
	联金所接口
	**/
	public function ljs_bind_user(){
        $id = I('post.id');
		$userid = I('post.userid');
		$app_token = I('post.token');
        import("Api.Lianjinsuo");
        $ljs = new \Lianjinsuo();
        $info = $ljs->login();
        $login_info = json_decode($info,true);
        if($login_info['date']['token']){
            $token = $login_info['date']['token'];
			
			 if($userid || $app_token){
                    $map['id'] = $userid;
                    $userinfo = D('User')->get_one($map);
                    $app_token_v = $userinfo['app_token'];
                    if($app_token_v == $app_token){
                        $phone = $userinfo['user_phone'];
                        $realname = $userinfo['realname'];
                        $idcard = $userinfo['idcard'];
                        $user_id = $userinfo['id'];
                        $datas['app_token'] ='';
                        $where['id'] = $userid; 
                        D('User')->edit($where,$datas);
                    }
            }else{
                $phone = $_SESSION['user']['user_phone'];
                $realname = $_SESSION['user']['realname'];
                $idcard = $_SESSION['user']['idcard'];
                $user_id = $_SESSION['user']['id'];
            }
			
            $r = $ljs->bind_user($token,$phone,urlencode($realname),$idcard,$user_id);
            //print_r($r);die;
            $r = json_decode($r,true);
            if($r['status']==200 or  $r['status']=="201"){

                $map = array();
                $map['user_id'] = $userid;
                $map['platform_id'] = 1;
                $u_plat = D('UserPlatform')->where($map)->find();
                if(!$u_plat && $userid){
                    $map = array();
                    $map['id'] = $userid;
                    $user_info = D('User')->where($map)->find();

                    $data['user_id'] = $userid;
                    $data['add_time'] = time();
                    $data['platform_id'] = 2;
                    $data['referral'] =$user_info['referral'];
                    D('UserPlatform')->add($data);
                }

                $data = array('phone'=>$phone,'realname'=>$realname,'idcard'=>$idcard,'UserId'=>$userid);

                $rsa_data = $this->rsa_privencrypt($data);
               //  print_r($rsa_data);die;
                $info = $ljs->login();
                $login_info = json_decode($info,true);
                $token = $login_info['date']['token'];
                $url = 'https://www.uf-club.com/jrj/login?token='.$token.'&datakey=login';
               //$url="http://new.8hjie.com/Home/Platform/test";
                 if($r['status']==200){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
                }elseif($r['status']=='201'){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
                }
                $this->ajaxReturn($data);
            }


        }
      
    }
	
	

    //果樹賬號綁定
     public function gs_bind_user(){
        $id = I('post.id');
        $userid = I('post.userid');
        $app_token = I('post.token');
        import("Api.Guoshu");
        $gs = new \Guoshu();
        $info = $gs->login();
        $login_info = json_decode($info,true);
		//echo $info;die;
        if($login_info['data']['token']){
			
            $token = $login_info['data']['token'];
            if($userid || $app_token){
                    $map['id'] = $userid;
                    $userinfo = D('User')->get_one($map);
                    $app_token_v = $userinfo['app_token'];
                    if($app_token_v == $app_token){
                        $phone = $userinfo['user_phone'];
                        $realname = $userinfo['realname'];
                        $idcard = $userinfo['idcard'];
                        $user_id = $userinfo['id'];
                        $datas['app_token'] ='';
                        $where['id'] = $userid; 
                        D('User')->edit($where,$datas);
                    }
            }else{
                $phone = $_SESSION['user']['user_phone'];
                $realname = $_SESSION['user']['realname'];
                $idcard = $_SESSION['user']['idcard'];
                $user_id = $_SESSION['user']['id'];
            }
            
            

            $r = $gs->bind_user($token,$phone,$realname,$idcard,$user_id);
            $r = json_decode($r,true);
            //返回信息开始登陆
             //返回信息开始登陆
            if($r['status']==200 or $r['status']==201){
                $map = array();
                $map['user_id'] = $user_id;
                $map['platform_id'] = 8;
                $u_plat = D('UserPlatform')->where($map)->find();
                if(!$u_plat  && $user_id){
                    $map = array();
                    $map['id'] = $user_id;
                    $user_info = D('User')->where($map)->find();
                        $data['user_id'] = $user_id;
                        $data['add_time'] = time();
                        $data['platform_id'] = 8;
                        $data['referral'] =$user_info['referral'];
                        $map = array();
                        $map['platform_id'] = 8;
                        $map['user_id'] = $user_id;
                        $res =  D('UserPlatform')->where($map)->find();
                           if(!$res){
                                D('UserPlatform')->add($data);
                           }
                }
                $data = array('phone'=>$phone,'realname'=>$realname,'idcard'=>$idcard,'user_id'=>$user_id);
                //print_r($data);
                $rsa_data = $this->rsa_privencrypt($data);
                // print_r($rsa_data);die;
                $url = 'https://www.goodsure.cn/jiekou/8hjie/index.php?token='.$token.'&datakey=login';
                if($r['status']==200){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
                }elseif($r['status']==201){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
                }
                $this->ajaxReturn($data);
            } 

        }
      
    }
    
    //中融投账号绑定
     public function zr_bind_user(){
        $id = I('post.id');
        import("Api.Zhongtourong");
        $zr = new \Zhongtourong();
        $info = $zr->login();
        $login_info = json_decode($info,true);
        if($login_info['data']['token']){
            $token = $login_info['data']['token'];
            $phone = $_SESSION['user']['user_phone'];
            $realname = $_SESSION['user']['realname'];
            $idcard = $_SESSION['user']['idcard'];
            $user_id = $_SESSION['user']['id'];
            $r = $zr->bind_user($token,$phone,$realname,$idcard,$user_id);
            $r = json_decode($r,true);

            //返回信息开始登陆
             //返回信息开始登陆
            if($r['status']==200 or $r['status']==201  or $r['status']==203){
                $map = array();
                $map['user_id'] = $user_id;
                $map['platform_id'] = 11;
                $u_plat = D('UserPlatform')->where($map)->find();
                if(!$u_plat  && $user_id){
                    $map = array();
                    $map['id'] = $user_id;
                    $user_info = D('User')->where($map)->find();
                        $data['user_id'] = $user_id;
                        $data['add_time'] = time();
                        $data['platform_id'] = 8;
                        $data['referral'] =$user_info['referral'];
                        $map = array();
                        $map['platform_id'] = 8;
                        $map['user_id'] = $user_id;
                        $res =  D('UserPlatform')->where($map)->find();
                           if(!$res){
                                D('UserPlatform')->add($data);
                           }
                }
                $data = array('phone'=>$phone,'realname'=>$realname,'idcard'=>$idcard,'user_id'=>$user_id);
                //print_r($data);
                $rsa_data = $this->rsa_privencrypt($data);
                // print_r($rsa_data);die;
                    $url = 'https://www.topzrt.com/wap/index.php?ctl=eight_finance&act_2=jinrongjie_login';
                
                

                if($r['status']==200 or $r['status']==201){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
                }elseif($r['status']==203){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
                }
                $this->ajaxReturn($data);
            } 

        }
      
    }

    //万盈账号绑定
    public function wy_bind_user(){
        $id = I('post.id');
        $user_id = I('post.userid');
        $app_token = I('post.token');
        import("Api.Wanying");
        $wy = new \Wanying();
        $info = $wy->login();

        $login_info = json_decode($info,true);
        if($login_info['data']['token']){
            $token = $login_info['data']['token'];
            if($userid || $app_token){
                    $map['id'] = $user_id;
                    $userinfo = D('User')->get_one($map);
                    $app_token_v = $userinfo['app_token'];
                    if($app_token_v == $app_token){
                        $phone = $userinfo['user_phone'];
                        $realname = $userinfo['realname'];
                        $idcard = $userinfo['idcard'];
                        $userid = $userinfo['id'];
                        $datas['app_token'] ='';
                        $where['id'] = $userid; 
                        D('User')->edit($where,$datas);
                    }
                    
            }else{
                $phone = $_SESSION['user']['user_phone'];
                $realname = $_SESSION['user']['realname'];
                $idcard = $_SESSION['user']['idcard'];
                $userid = $_SESSION['user']['id'];
            }

            $r = $wy->bind_user($token,$phone,$realname,$idcard,$userid);
           // print_r($r);
            $r = json_decode($r,true);
            if($r['status']==200 or $r['status']=="-101" or $r['status']=="-103"){
                $map = array();
                $map['user_id'] = $userid;
                $map['platform_id'] = 2;
                $u_plat = D('UserPlatform')->where($map)->find();
                if(!$u_plat && $userid){
                    $map = array();
                    $map['id'] = $userid;
                    $user_info = D('User')->where($map)->find();

                    $data['user_id'] = $userid;
                    $data['add_time'] = time();
                    $data['platform_id'] = 2;
                    $data['referral'] =$user_info['referral'];
                    D('UserPlatform')->add($data);
                }

                $data = array('phone'=>$phone,'realname'=>$realname,'idcard'=>$idcard,'UserId'=>$userid);
                $rsa_data = $this->rsa_pubencrypt($data);
                $info = $wy->login();
                $login_info = json_decode($info,true);
                $token = $login_info['data']['token'];
                $url = 'https://www.wyjr168.com/ThirdParty/JinrongjieLogin?token='.$token.'&datakey=login';
               //$url="http://new.8hjie.com/Home/Platform/test";
                 if($r['status']==200 or $r['status']=='-103'){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
                }elseif($r['status']=='-101'){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
                }
                $this->ajaxReturn($data);
            }


        }
      
    }
    
    
    //普惠账号绑定
    public function ph_bind_user(){
        $id = I('post.id');
        $user_id = I('post.userid');
        $app_token = I('post.token');
        import("Api.Puhuiyuntong");
        $ph = new \Puhuiyuntong();
        $info = $ph->login();
        $login_info = json_decode($info,true);
		
        if($login_info['data']['token']){
            $token = $login_info['data']['token'];
            if($userid || $app_token){
                    $map['id'] = $user_id;
                    $userinfo = D('User')->get_one($map);
                    $app_token_v = $userinfo['app_token'];
                    if($app_token_v == $app_token){
                        $phone = $userinfo['user_phone'];
                        $nickname = $userinfo['nickname'].rand('1','99');
                        $idcard = $userinfo['idcard'];
                        $userid = $userinfo['id'];
                        $datas['app_token'] ='';
                        $where['id'] = $userid; 
                        D('User')->edit($where,$datas);
                    }
                        
                    
            }else{
                $phone = $_SESSION['user']['user_phone'];
                $nickname = $_SESSION['user']['nickname'].rand('1000','9999');
                $idcard = $_SESSION['user']['idcard'];
                $userid = $_SESSION['user']['id'];
            }
            

            $r = $ph->bind_user($token,$phone,urlencode($nickname),$idcard,$userid);
            $r = json_decode($r,true);
			
            if($r['Status']==200 or $r['Status']==202 or $r['Status']=="1003" or $r['Status']=="201"){

                $map = array();
                $map['user_id'] = $userid;
                $map['platform_id'] = 2;
                $u_plat = D('UserPlatform')->where($map)->find();
                if(!$u_plat && $userid){
                    $map = array();
                    $map['id'] = $userid;
                    $user_info = D('User')->where($map)->find();

                    $data['user_id'] = $userid;
                    $data['add_time'] = time();
                    $data['platform_id'] = 2;
                    $data['referral'] =$user_info['referral'];
                    D('UserPlatform')->add($data);
                }

                $data = array('phone'=>$phone,'realname'=>$realname,'idcard'=>$idcard,'UserId'=>$userid);
                $rsa_data = $this->rsa_pubencrypt($data);
                $info = $ph->login();
                $login_info = json_decode($info,true);
                $token = $login_info['data']['token'];
                $url = 'https://www.phyt88.com/v3/finance/dologin?token='.$token.'&datakey=login';
               //$url="http://new.8hjie.com/Home/Platform/test";
                 if($r['Status']==200 or $r['Status']==202 or $r['Status']=='1003'){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
                }elseif($r['Status']=='201'){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
                }
                $this->ajaxReturn($data);
            }


        }
      
    }
    
    //微金在线账号绑定
    public function wj_bind_user(){ 
        $id = I('post.id');
        import("Api.Weijinoline");
        $wj = new \Weijinoline();
        $info = $wj->login();
        $login_info = json_decode($info,true);
        if($login_info['data']['token']){
            $token = $login_info['data']['token'];
            $phone = $_SESSION['user']['user_phone'];
            $realname = $_SESSION['user']['realname'];
            $idcard = $_SESSION['user']['idcard'];
            $user_id = $_SESSION['user']['id'];
            $r = $wj->bind_user($token,$phone,$realname,$idcard,$user_id);
            $r = json_decode($r,true);
            //返回信息开始登陆
             //返回信息开始登陆
            if($r['status']==200 or $r['status']==201  or $r['status']==202){
                $map = array();
                $map['user_id'] = $user_id;
                $map['platform_id'] = 11;
                $u_plat = D('UserPlatform')->where($map)->find();
                if(!$u_plat  && $user_id){
                    $map = array();
                    $map['id'] = $user_id;
                    $user_info = D('User')->where($map)->find();
                        $data['user_id'] = $user_id;
                        $data['add_time'] = time();
                        $data['platform_id'] = 8;
                        $data['referral'] =$user_info['referral'];
                        $map = array();
                        $map['platform_id'] = 8;
                        $map['user_id'] = $user_id;
                        $res =  D('UserPlatform')->where($map)->find();
                           if(!$res){
                                D('UserPlatform')->add($data);
                           }
                }
                $data = array('phone'=>$phone,'realname'=>$realname,'idcard'=>$idcard,'UserId'=>$user_id);
                //print_r($data);
                $rsa_data = $this->rsa_privencrypt($data);
                // print_r($rsa_data);die;
                $url = 'https://www.weijinzaixian.com/svc/bahaojie/login?token='.$token.'&datakey=login';

                if($r['status']==200 or $r['status']==202){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
                }elseif($r['status']==201){
                    $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
                }
                $this->ajaxReturn($data);
            } 

        }
      
    } 
    
	//红岭账号绑定
    public function hl_bind_user(){ 
        $id = I('post.id');
       $app_token = I('post.token');
	   $userid = I('post.userid');
        import("Api.Hongling");
		
        $hl = new \Hongling();
		// echo 1;die;	
            if($userid || $app_token){
				 
                    $map['id'] = $userid;
                    $userinfo = D('User')->get_one($map);
                    $app_token_v = $userinfo['app_token'];
					//echo $app_token_v;die;
                    if($app_token_v == $app_token){
                        $phone = $userinfo['user_phone'];
                        $realname = $userinfo['realname'];
                        $idcard = $userinfo['idcard'];
                        $userid = $userinfo['id'];
                        $datas['app_token'] ='';
                        $where['id'] = $userid; 
                        D('User')->edit($where,$datas);
					}
			}else{
						$phone = $_SESSION['user']['user_phone'];
						$realname = $_SESSION['user']['realname'];
						$idcard = $_SESSION['user']['idcard'];
						$userid = $_SESSION['user']['id'];
			}
				
            //echo $userid;die;
            $info = $hl->bind_user($phone,urlencode($realname),$idcard,$userid);
			//echo $info;die;
            $info = json_decode($info,true);
			//print_r($info);die;
            //返回信息开始登陆
             //返回信息开始登陆
            if($info['status']==200){
                $map = array();
                $map['user_id'] = $userid;
                $map['platform_id'] = 18;
                $u_plat = D('UserPlatform')->where($map)->find();
                if(!$u_plat  && $userid){
                    $map = array();
                    $map['id'] = $userid;
                    $user_info = D('User')->where($map)->find();
                        $data['user_id'] = $userid;
                        $data['add_time'] = time();
                        $data['platform_id'] = 8;
                        $data['referral'] =$user_info['referral'];
                        $map = array();
                        $map['platform_id'] = 8;
                        $map['user_id'] = $userid;
                        $res =  D('UserPlatform')->where($map)->find();
					   if(!$res){
							D('UserPlatform')->add($data);
					   }
                }
				$consumerId ='17111500001';
                $time = date('YmdHis',time());
                $data = array('time'=>$time,'phone'=>$phone,'UserId'=>$userid);
                $rsa_data = $this->rsa_pubencrypt($data);              
                $sign = "430e66a9244e36058f8acfcf68e5c5c2&consumerId=".$consumerId."&data=".$rsa_data;
                $sign = md5($sign);
                 $url = 'https://sso.my089.com/sso/8hjrj/login?consumerId='.$consumerId.'&data='.$rsa_data.'&sign='.$sign;
				  $data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>0);
            }else{
				$data = array('err'=>0,'data'=>$rsa_data,'url'=>$url,'msg'=>1);
			}
                $this->ajaxReturn($data);
    } 
    
    
        //私钥加密公钥解密
    public function rsa_privencrypt($data){
        $id = I('post.id');
        import("Api.Rsa");
        $data = json_encode($data);
         $pubKey = '/data/www/new8hjie/App/Home/Public/ssl/rsa_public_key.pem'; 
        $privKey = '/data/www/new8hjie/App/Home/Public/ssl/rsa_private_key.pem';
        
        import("Api.Rsa");
        $m = new \Rsa($pubKey, $privKey);
        $x = $m->privEncrypt($data); 
        $x = $m->privEncrypt($data); 
        return($x);
       
    }


    //公钥加密公私钥解密

    public function rsa_pubencrypt($data){
        $id = I('post.id');
        import("Api.Rsa"); 
        $data = json_encode($data);
        ////////////////////////////////////// 
        $pubKey = '/data/www/new8hjie/App/Home/Public/ssl/rsa_public_key.pem'; 
        $privKey = '/data/www/new8hjie/App/Home/Public/ssl/rsa_private_key.pem';
        // $m = new RSA($pubfile, $prifile); 
        $m = new \Rsa($pubKey, $privKey);
        $x = $m->pubEncrypt($data); 
        return($x);
       
    }
    
     //我要评测平台
    public function plat_qusetion() {
        if(IS_POST){
            $postdata = I('post.');
            if($postdata['plat_name']) {
                $postdata['plat_invest'] = implode(',',$postdata['plat_invest']);
                $postdata['plat_bad'] = implode(',',$postdata['plat_bad']);
                $res = M('PlatformQuestion')->add($postdata);
                if($res) {
                    $data = array('err'=>0,'msg'=>"提交成功");
                } else {
                    $data = array('err'=>1,'msg'=>"提交失败");
                }
                $this->ajaxReturn($data);
            } else {
                $data = array('err'=>2,'msg'=>"平台名称不能为空");
            }
        } else {
            $this->display();
        }
    }



    public function bind_platform(){
        $id = I('get.id');
        $map['id'] = $id;
        $info = D('Platform')->get_one($map);
        $map = array();
        $map['id'] = $_SESSION['user']['id'];
        $user_info = D('User')->get_one($map);

    
        $map = array();
        $map['id'] =  $_SESSION['user']['id'];
        $user_info = D('User')->where($map)->find();

        $data['user_id'] =  $_SESSION['user']['id'];
        $data['add_time'] = time();
        $data['platform_id'] =$id;
        $data['referral'] =$user_info['referral'];

        $map = array();
        $map['platform_id'] =$id;
        $map['user_id'] = $_SESSION['user']['id'];
        $res =  M('UserPlatform')->where($map)->find();
       if(!$res && $_SESSION['user']['id']){
            D('UserPlatform')->add($data);
       }
                


        $this->assign('info',$info);
        $this->assign('user_info',$user_info);
        $this->display();
    }

}